//
//  MatchTorgetherView.m
//  Meow
//
//  Created by YHQ on 15/11/5.
//  Copyright © 2015年 SRDZ. All rights reserved.
//

#import "MatchTorgetherView.h"
#import "Header.h"

#define IMGWIDTH 100.0f
#define BTNHEIGHT 40.0f

@interface MatchTorgetherView ()

@property(nonatomic,strong)UIWindow *overlayWindow;
@property(nonatomic,strong)UIView *bgView;

@property(nonatomic,strong)UIImageView *myHeadImageView,*otherHeadImageView;
@property(nonatomic,strong)UILabel *congrationLabel,*namesLabel;
@property(nonatomic,strong)UIView *backView,*chatView;
@end

@implementation MatchTorgetherView

+(MatchTorgetherView *)sharedMatchView
{
    static MatchTorgetherView *matchView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        matchView = [[MatchTorgetherView alloc] init];
    });
    return matchView;
}

-(instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

-(void)showWithImage:(NSString *)imageName targetName:(NSString *)userName
{
    [self dismiss];
    self.chatView.layer.position = CGPointMake(WindowWidth, WindowHeight / 2 + 100);
    self.backView.layer.position = CGPointMake(-(WindowWidth - 120), WindowHeight / 2 + 100 + BTNHEIGHT + 20);
   // User *user = [[UserManager sharedHelper] inquireUser];
    //[self.myHeadImageView sd_setImageWithURL:[NSURL URLWithString:user.headImg]];
    [self.otherHeadImageView sd_setImageWithURL:[NSURL URLWithString:imageName]];
    [self addSubview:self.bgView];
    [self addSubview:self.myHeadImageView];
    [self addSubview:self.otherHeadImageView];
    [self addSubview:self.congrationLabel];
    
    self.namesLabel.text = [NSString stringWithFormat:@"你与%@互相点赞了对方,聊聊看吧",userName];
    [self addSubview:self.namesLabel];
    [self addSubview:self.chatView];
    [self addSubview:self.backView];
    
    [self addAnimation];
    [self.overlayWindow addSubview:self];
    [self.overlayWindow makeKeyAndVisible];
    
}

-(void)addAnimation
{
    CABasicAnimation *headAnimat = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    headAnimat.fromValue = [NSNumber numberWithFloat:1.0f];
    headAnimat.toValue = [NSNumber numberWithFloat:1.2f];
    headAnimat.fillMode = kCAFillModeForwards;
    headAnimat.autoreverses = YES;
    headAnimat.duration = 1.0f;
    [_otherHeadImageView.layer addAnimation:headAnimat forKey:@"HEAD_ANIMATION1"];
    
    CABasicAnimation *headAnimat2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    headAnimat2.fromValue = [NSNumber numberWithFloat:1.0f];
    headAnimat2.toValue = [NSNumber numberWithFloat:1.2f];
    headAnimat2.fillMode = kCAFillModeForwards;
    headAnimat2.autoreverses = YES;
    headAnimat2.duration = 1.0f;
    [_myHeadImageView.layer addAnimation:headAnimat2 forKey:@"HEAD_ANIMATION2"];
    
    
    [UIView animateWithDuration:2.0f delay:0 usingSpringWithDamping:0.4 initialSpringVelocity:5.0f options:UIViewAnimationOptionCurveLinear animations:^{
        CGPoint center = _backView.layer.position;
        center.x = WindowWidth / 2;
        _backView.center = center;
    } completion:^(BOOL finished) {
        
    }];
    
    [UIView animateWithDuration:2.0f delay:0 usingSpringWithDamping:0.4 initialSpringVelocity:5.0f options:UIViewAnimationOptionCurveLinear animations:^{
        CGPoint center = _chatView.layer.position;
        center.x = WindowWidth / 2;
        _chatView.center = center;
    } completion:^(BOOL finished) {
        
    }];
}


-(void)dismiss
{
    [self.myHeadImageView removeFromSuperview];
    [self.myHeadImageView.layer removeAllAnimations];
    [self.otherHeadImageView removeFromSuperview];
    [self.otherHeadImageView.layer removeAllAnimations];
    [self.congrationLabel removeFromSuperview];
    [self.namesLabel removeFromSuperview];
    [self.chatView removeFromSuperview];
    [self.backView removeFromSuperview];
    [self.backView.layer removeAllAnimations];
    [self.bgView removeFromSuperview];
    [self.bgView.layer removeAllAnimations];
    self.overlayWindow = nil;
    [[UIApplication sharedApplication].windows enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(UIWindow *window, NSUInteger idx, BOOL *stop) {
        if ([window isKindOfClass:[UIWindow class]] && window.windowLevel == UIWindowLevelNormal && ![[window class] isEqual:[MatchTorgetherView class]]) {
            [window makeKeyWindow];
            *stop = YES;
        }
    }];
}

-(UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:self.bounds];
        _bgView.backgroundColor = [UIColor blackColor];
        _bgView.alpha = 0.7f;
        _bgView.userInteractionEnabled = YES;
        
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
//        _bgView.userInteractionEnabled = YES;
//        [_bgView addGestureRecognizer:tap];
    }
    return _bgView;
}

-(UIImageView *)myHeadImageView
{
    if (!_myHeadImageView) {
        _myHeadImageView = [[UIImageView alloc] init];
        _myHeadImageView.frame = CGRectMake((WindowWidth - 2 * IMGWIDTH +  20) / 2, 80, IMGWIDTH, IMGWIDTH);
        _myHeadImageView.layer.cornerRadius = IMGWIDTH/2;
        _myHeadImageView.clipsToBounds = YES;
        _myHeadImageView.layer.borderColor = [UIColor whiteColor].CGColor;
        _myHeadImageView.layer.borderWidth = 2.0f;
        
    }
    return _myHeadImageView;
}

-(UIImageView *)otherHeadImageView
{
    if (!_otherHeadImageView) {
        _otherHeadImageView = [[UIImageView alloc] init];
        _otherHeadImageView.frame = CGRectMake(CGRectGetMaxX(self.myHeadImageView.frame) - 20, 80, IMGWIDTH, IMGWIDTH);
        _otherHeadImageView.layer.cornerRadius = IMGWIDTH / 2;
        _otherHeadImageView.clipsToBounds = YES;
        _otherHeadImageView.layer.borderWidth = 2.0f;
        _otherHeadImageView.layer.borderColor = [UIColor whiteColor].CGColor;
        

    }
    return _otherHeadImageView;
}

-(UILabel *)congrationLabel
{
    if (!_congrationLabel) {
        _congrationLabel = [[UILabel alloc] init];
        _congrationLabel.frame = CGRectMake(10, WindowHeight / 2 - 50, WindowWidth - 20, 25);
        _congrationLabel.font = [UIFont systemFontOfSize:15.0f];
        _congrationLabel.textAlignment = NSTextAlignmentCenter;
        _congrationLabel.textColor = [UIColor whiteColor];
        _congrationLabel.text = @"恭喜恭喜！";
    }
    return _congrationLabel;
}

-(UILabel *)namesLabel
{
    if (!_namesLabel) {
        _namesLabel = [[UILabel alloc] init];
        _namesLabel.frame = CGRectMake(10, WindowHeight / 2 - 25, WindowWidth - 20, 25);
        _namesLabel.font = [UIFont systemFontOfSize:15.0f];
        _namesLabel.textAlignment = NSTextAlignmentCenter;
        _namesLabel.textColor = [UIColor whiteColor];
    }
    return _namesLabel;
}

-(UIView *)backView
{
    if (!_backView) {
        
        _backView = [[UIView alloc] initWithFrame:CGRectMake(-WindowWidth + 120, WindowHeight / 2 + 100 + BTNHEIGHT + 20, WindowWidth - 120, BTNHEIGHT)];
        _backView.layer.cornerRadius = 5.0f;
        _backView.clipsToBounds = YES;
        _backView.layer.borderColor = [UIColor whiteColor].CGColor;
        _backView.layer.borderWidth = 1;
        NSString *str = @"再喵喵";
        CGFloat width = [str inquireSizeWithFont:14.0f Widths:CGFLOAT_MAX Heights:20].size.width;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((_backView.frame.size.width - width - 30)/2, (_backView.frame.size.height - 25)/2, 25, 25)];
        imageView.image = [UIImage imageNamed:@"xiao xi"];
        [_backView addSubview:imageView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) + 5, imageView.frame.origin.y, width, imageView.frame.size.height)];
        label.text = str;
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:14.0f];
        [_backView addSubview:label];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
        _backView.userInteractionEnabled = YES;
        [_backView addGestureRecognizer:tap];
    }
    return _backView;
}

-(UIView *)chatView
{
    if (!_chatView) {
        
        _chatView = [[UIView alloc] initWithFrame:CGRectMake(WindowWidth, WindowHeight / 2 +100, WindowWidth - 120, BTNHEIGHT)];
        _chatView.layer.cornerRadius = 5.0f;
        _chatView.clipsToBounds = YES;
        _chatView.backgroundColor = darkRed;
        
        NSString *str = @"聊聊看";
        CGFloat width = [str inquireSizeWithFont:14.0f Widths:CGFLOAT_MAX Heights:20].size.width;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((_chatView.frame.size.width - width - 30)/2, (_chatView.frame.size.height - 25)/2, 25, 25)];
        imageView.image = [UIImage imageNamed:@"M_zhuazi"];
        [_chatView addSubview:imageView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) + 5, imageView.frame.origin.y, width, imageView.frame.size.height)];
        label.text = str;
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:14.0f];
        [_chatView addSubview:label];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chatAction)];
        _chatView.userInteractionEnabled = YES;
        [_chatView addGestureRecognizer:tap];
    }
    return _chatView;
}





-(void)chatAction
{
    [self dismiss];
    if (self.chatBlock) {
        self.chatBlock();
    }
}

-(UIWindow *)overlayWindow
{
    if (!_overlayWindow) {
        _overlayWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _overlayWindow.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        _overlayWindow.backgroundColor = [UIColor clearColor];
        _overlayWindow.windowLevel = UIWindowLevelAlert;
        _overlayWindow.userInteractionEnabled = YES;
    }
    return _overlayWindow;
}



@end
